Skip to content

[OpenVINO backend] Support for numpy.det using Faddeev-LeVerrier algorithm#22355

Open
andersendsa wants to merge 2 commits intokeras-team:masterfrom
andersendsa:openvino-det
Open

[OpenVINO backend] Support for numpy.det using Faddeev-LeVerrier algorithm#22355
andersendsa wants to merge 2 commits intokeras-team:masterfrom
andersendsa:openvino-det

Conversation

@andersendsa
Copy link
Contributor

Details :

Implement numpy.det operation for the OpenVINO backend.

Closes issue : openvinotoolkit/openvino#34510

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces the long-awaited implementation of the numpy.det function for the OpenVINO backend. By leveraging the Faddeev-LeVerrier algorithm and OpenVINO's graph operations, it enables Keras users to perform determinant calculations on tensors when using the OpenVINO runtime. This significantly expands the linear algebra capabilities available within the OpenVINO backend, resolving a previously unsupported operation and improving compatibility with NumPy's linear algebra API.

Highlights

  • Numpy.det implementation: The numpy.det operation has been fully implemented for the OpenVINO backend, replacing the previous NotImplementedError.
  • Faddeev-LeVerrier Algorithm: The determinant calculation utilizes the Faddeev-LeVerrier algorithm, implemented using OpenVINO's loop operation to compute the characteristic polynomial coefficients.
  • F64 Type Handling: A workaround has been included to convert f64 inputs to f32 during computation and then back to f64 for the output, addressing a constant folding bug in OpenVINO's CPU Loop for f64 types.
  • Test Enablement: The LinalgOpsCorrectnessTest::test_det has been removed from the excluded_concrete_tests.txt file, indicating that the determinant test is now expected to pass with this implementation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • keras/src/backend/openvino/excluded_concrete_tests.txt
    • Removed LinalgOpsCorrectnessTest::test_det from the list of excluded tests.
  • keras/src/backend/openvino/linalg.py
    • Added import for openvino module.
    • Implemented the det function using OpenVINO ops, based on the Faddeev-LeVerrier algorithm.
    • Included logic to handle f64 input types by converting to f32 and back to mitigate a known OpenVINO bug.
    • Utilized ov_opset.loop to iteratively compute coefficients for the characteristic polynomial.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the numpy.det operation for the OpenVINO backend using the Faddeev-LeVerrier algorithm. The implementation appears to be correct, handling batch dimensions and including a workaround for an f64 bug. The corresponding correctness test is also enabled. I have one suggestion to improve code readability by consolidating constant definitions.

Comment on lines +50 to +58
minus_1 = ov_opset.constant([-1], Type.i32).output(0)
minus_2 = ov_opset.constant([-2], Type.i32).output(0)

N_node_1d = ov_opset.gather(
a_shape, minus_1, ov_opset.constant(0, Type.i32).output(0)
).output(0)
N_node_scalar = ov_opset.squeeze(
N_node_1d, ov_opset.constant([0], Type.i32).output(0)
).output(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve readability and maintainability, it's good practice to define constants at the top of their scope. Here, you can define zero_i32 and zero_i32_vec and reuse them. This avoids creating the same constant inline multiple times later in the function.

    minus_1 = ov_opset.constant([-1], Type.i32).output(0)
    minus_2 = ov_opset.constant([-2], Type.i32).output(0)

    zero_i32 = ov_opset.constant(0, Type.i32).output(0)
    zero_i32_vec = ov_opset.constant([0], Type.i32).output(0)

    N_node_1d = ov_opset.gather(
        a_shape, minus_1, zero_i32
    ).output(0)
    N_node_scalar = ov_opset.squeeze(
        N_node_1d, zero_i32_vec
    ).output(0)

@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2026

Codecov Report

❌ Patch coverage is 92.72727% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.96%. Comparing base (95e74a9) to head (5650c8f).

Files with missing lines Patch % Lines
keras/src/backend/openvino/linalg.py 92.72% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #22355   +/-   ##
=======================================
  Coverage   82.95%   82.96%           
=======================================
  Files         595      595           
  Lines       66040    66095   +55     
  Branches    10305    10308    +3     
=======================================
+ Hits        54785    54836   +51     
- Misses       8639     8640    +1     
- Partials     2616     2619    +3     
Flag Coverage Δ
keras 82.79% <92.72%> (+<0.01%) ⬆️
keras-jax 60.78% <1.81%> (-0.05%) ⬇️
keras-numpy 54.98% <0.00%> (-0.05%) ⬇️
keras-openvino 49.15% <92.72%> (+0.04%) ⬆️
keras-tensorflow 62.01% <1.81%> (-0.05%) ⬇️
keras-torch 60.82% <1.81%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@andersendsa
Copy link
Contributor Author

hi @hertschuh this pr passes all the tests and is ready for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants